var HttpVuePlus = {
    load: function (code, url, resolve) {
        var instance = HttpVuePlus.newInstance(code, url, resolve);
        instance.load(code, url);
        instance.checkImport(); // 必须加上，不然无法正常显示
        return instance.code;
    },
    newInstance : function(code, url, _finish) {
        if(_finish == null) {
            console.log('url', url);
        }
        return {
            importSize: 0,
            loadImportSize: 0,
            code: '',
            isResolve: false,
            _finish: _finish,
            resolve() {
                if(this.isResolve) {
                    return;
                }
                this._finish(this.code);
                this.isResolve = true;
            },
            checkImport () {
                if(this.importSize == this.loadImportSize) {
                    console.log('加载完成');
                    this.resolve(this.code);
                    return true;
                }
                return false;
            },
            load (code, url) {
                // console.log('code', code);
                var lines = code.split('\n');
                for (var i = 0; i < lines.length; i++) {
                    lines[i] = this.loadLine(lines[i], url);
                }
                this.code = lines.join('\n');
            },
            loadLine (line, url) {
                if(line.indexOf('import') !== -1 && line.indexOf('from') !== -1) {
                    this.importSize ++;
                    return this.loadImport(line, url);
                } else if(line.indexOf('export default') !== -1) {
                    return this.loadExport(line, url);
                } else {
                    return line;
                }
            },
            loadExport (line) {
                // 语法转换
                return line.replace('export default', 'module.exports=');
            },
            urlDir(url) {
                return url.substring(0, url.lastIndexOf('/'));
            },
            /**
             * 加载行中的import
             * @param {String} line 
             * @param {String} url 
             * @returns 
             */
            loadImport (line, url) {
                var info = UrlParser.parse(url);
                var splits = line.split('from');
                var name = splits[0].replace('import', '').trim();
                if(window[name] != null) {
                    this.loadImportSize ++;
                    this.checkImport();
                    return;
                }
                var path = splits[1].replaceAll('\'', '')
                    .replaceAll(';', '')
                    .trim();
                // loadJs
                // import Test from './test/test.js';
                var urlDir = this.urlDir(url);
                var resourceUrl = urlDir + path.replace('.', '');

                // 判断资源类型
                console.log('resourceUrl', resourceUrl);
                var type = resourceUrl.split(".")[2];
                console.log('loadImport type', type);
                
                if(type == 'js') {

                    $.get(resourceUrl, (code) => {
                        var jsCode = this.loadJs(code, name);
                        this.loadImportSize ++;
                        eval(jsCode);
                        this.checkImport();
                        console.log('加载' + name + '成功');
                    }, 'text');

                } else if(type == 'vue') {
                    window[name] = httpVueLoader(resourceUrl);
                    console.log('load ' + name, window[name]);
                    this.loadImportSize ++;
                    this.checkImport();
                }
                    
                return '';
            },
            loadJs(js, name) {
                return js.replace('export default', 'window.' + name + ' = ');
            },
            loadScript( innerHTML, callback ){

                var script = document.createElement('script'),

                fn = callback || function(){};

                script.type = 'text/javascript';

                //IE

                if(script.readyState){

                    script.onreadystatechange = function(){

                        if( script.readyState == 'loaded' || script.readyState == 'complete' ){

                            script.onreadystatechange = null;

                            fn();

                        }

                    };

                }else{

                    //其他浏览器

                    script.onload = function(){

                        fn();

                    };

                }

                // script.src = url;
                script.innerHTML = innerHTML;

                document.getElementsByTagName('head')[0].appendChild(script);

            }
        }
    }
};

window.HttpVuePlus = HttpVuePlus;